// 这个vuex模块用来筛选用户是否有权限访问路由模块
import { constantRoutes, asyncRoutes } from '@/router'

const state = {
  routes: []
}

const mutations = {
  setRoutes(state, newRoutes) {
    state.routes = [...constantRoutes, ...newRoutes]
  }
}

const actions = {
  filterRoutes(context, menus) {
    const routes = asyncRoutes.filter(route => menus.indexOf(route.name) > -1)
    // console.log(routes)
    context.commit('setRoutes', routes)
    // 将筛选出的路由数组返回，在路由合并时进行加入
    return routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
